import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:xm_app/common/http/api_response.dart';
import 'package:xm_app/common/logger/LoggerUtil.dart';
import 'package:xm_app/dao/VodDao.dart';
import 'package:xm_app/model/HomeData.dart';

class TabsController extends GetxController {
  final recommendData = HomeData().obs;
  final hotIndex = 0.obs;
  EasyRefreshController easyRefreshController = EasyRefreshController();

  @override
  void onReady() {
    super.onReady();
    getRecommendData();
  }

  getRecommendData() async {
    ApiResponse res = await VodDao().getHomeList();
    if (res.status == Status.ERROR) {
      SmartDialog.showToast(res.exception.message);
      easyRefreshController.finishRefresh(success: false);
      return;
    }
    easyRefreshController.finishRefresh(success: true);
    logInfo("getRecommendData finishRefresh");
    recommendData.value = res.data;
    hotIndex.value = 0;
  }

  void refreshCurrentHotPageData() {
    int index = hotIndex.value;
    int hotGroup = recommendData.value.h.length ~/ 6;
    hotIndex.value = index >= hotGroup - 1 ? 0 : index + 1;
  }

  List<HomeDataItem> getCurrentHotPageData() {
    int index = hotIndex.value;
    var tempHotPageDatas = List<HomeDataItem>();
    if (recommendData.value.h == null) {
      return tempHotPageDatas;
    }
    tempHotPageDatas.addAll(recommendData.value.h);
    return tempHotPageDatas.sublist(index * 6, index * 6 + 6);
  }
}
